package com.budwk.app.sqfwV2.generator.lanhuadu.pdf;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BasePdfParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.component.ProblemComponent;
import com.budwk.app.sqfwV2.component.SignImageComponent;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.enums.lanHuaDu.LanHuaDuFileServiceEnum;
import com.budwk.app.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 责令限期整改指令书，版本号1.0
 */

@Slf4j
public class lhdOrderLetterDocGenerator extends BaseTemplatePdfGenerator{

    /**
     * 当该场面积>300平米时，且存在“隐患问题”，“整改时限”为“限期整改”时，生成该文档。
     *      1 场所面积 > 300
     *      2 存在“隐患问题”，“整改时限”为“限期整改”
     * @param pdfDTO
     * @return
     */
    @Override
    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        if (pdfDTO.isDownTemplate()) {
            return true;
        }
        FireSafetyOnDayingRoadPdfDTO roadPdfDTO = ProblemComponent.getSafetyPdfDTO(pdfDTO);
        // 判断场所面积
        String placeArea = roadPdfDTO.getPlaceArea();
        if (StringUtils.isEmpty(placeArea)) {
            return false;
        }
        if(new BigDecimal(roadPdfDTO.getPlaceArea()).compareTo(new BigDecimal(300)) < 1){
            return false;
        }
        // “整改时限”为“限期整改
        if (roadPdfDTO.getIsRectified() == 3) {
            return true;
        }
        return false;
    }

    @Override
    public String getTemplateName() {
        return LanHuaDuFileServiceEnum.D.getFilePath();
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, SqfwPdfCreateCommonBO pdfDTO) {
        if (pdfDTO.isDownTemplate()) {  // 下载模板
            buildNullData(paramMap);
            return;
        }
        FireSafetyOnDayingRoadPdfDTO roadPdfDTO = DayingRoadPdfComponent.getFireSafetyOnDayingRoadPdfDTO(paramMap, pdfDTO);
        BasePdfParamDTO basePdfParam = pdfDTO.getBasePdfParamDTO();
        paramMap.put("placeName", StrUtil.formatStr(roadPdfDTO.getPlaceName()));
        paramMap.put("county", StrUtil.formatStr(basePdfParam.getCounty()));

        List<String> hidderStr = getHidderStr(roadPdfDTO.getExistDangerStr());
        // 问题项
        List<String> pNos = new ArrayList<>();
        for (int i = 0; i < hidderStr.size(); i++) {
            pNos.add(String.valueOf(i+1));
        }
        paramMap.put("text", String.join("；", hidderStr));
        paramMap.put("pnos", String.join(",", pNos));
        // 整改时间
        paramMap.put("rtime", LocalDateTimeUtil.format(roadPdfDTO.getRectificationDate(), DatePattern.CHINESE_DATE_PATTERN));

        BasePdfParamDTO basePdfParamDTO = pdfDTO.getBasePdfParamDTO();
        String peers = StrUtil.formatStr(basePdfParamDTO.getCreateUserName());
        if (StringUtils.isNotEmpty(roadPdfDTO.getPeers())) {
            peers = String.format("%s%s%s", StrUtil.formatStr(basePdfParamDTO.getCreateUserName()), ",", roadPdfDTO.getPeers());
        }
        paramMap.put("peers", peers);
        SignImageComponent.signWord(paramMap, null, roadPdfDTO.getCheckedUnitLeader(), pdfDTO.getBasePdfParamDTO().getCreateUserName(), "checkedUnitLeader", "checkedUnitLeaderStr");
        paramMap.put("mobile", StrUtil.formatStr(roadPdfDTO.getOperatorTelephone()));

        // {市}{区}{街道}
        String area = String.format("%s%s%s", StrUtil.formatZhiShu(basePdfParam.getCity()), StrUtil.formatZhiShu(basePdfParam.getDistrict()), StrUtil.formatZhiShu(basePdfParam.getCounty()));
        paramMap.put("sqj", area);
        paramMap.put("fillDate", DateUtil.format(basePdfParam.getFillDate(), DatePattern.CHINESE_DATE_PATTERN));
    }

    private void buildNullData(Map<String, Object> paramMap) {
        paramMap.put("placeName", "");
        paramMap.put("text", "");
        paramMap.put("pnos", "");
        // 整改时间
        paramMap.put("rtime", "_年_月_日");
        paramMap.put("peers", "");
        paramMap.put("mobile", "");
        paramMap.put("sqj", "XX区XX街道");
        paramMap.put("checkedUnitLeader", null);
        paramMap.put("checkedUnitLeaderStr", "");
        paramMap.put("county", "XX街道");
        paramMap.put("fillDate", DateUtil.format(new Date(), DatePattern.CHINESE_DATE_PATTERN));
    }

    private List<String> getHidderStr(String existDangerStr) {
        String str = existDangerStr.replaceAll("\n", "").replaceAll("\\d+", "");
        String[] split = str.split(";");
        List<String> list = new ArrayList<>();
        int index = 1;
        for (String s : split) {
            list.add(index + s);
            index ++;
        }
        return list;
    }
}
